package controller

import (
	"context"
	"gin-service/lib/GinSrv/GinResp"
	"gin-service/lib/GinSrv/GinRouter"
	"gin-service/lib/GormSrv"
	"gin-service/lib/GormSrv/model"
	"gin-service/lib/MongoSrv"
	"github.com/gin-gonic/gin"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
)

//erc的采购管理模块
func ErcSupplierController(router GinRouter.Router) {
	routerCtx := context.Background()

	router.InitRouter()
	response := new(GinResp.RespImpl)

	//erc客户发送采购订单供应商
	//user客户 client供应商
	router.Post("/sendPurchaseOrder", func(ctx *gin.Context) {
		type Payload struct {
			PurchaseOrderList []bson.M
		}
		type ReqBody struct {
			Header interface{}
			Data Payload
		}
		var reqBody ReqBody
		GinRouter.ThrowError(ctx.BindJSON(&reqBody))

		insertCount := len(reqBody.Data.PurchaseOrderList)
		if insertCount == 0 {
			panic("no purchase order")
		}

		insertArray := make([]interface{}, insertCount)
		for index, item := range reqBody.Data.PurchaseOrderList {
			itemElement := bson.D{}
			for key, val := range item {
				itemElement = append(itemElement, bson.E{Key: key, Value: val})
			}
			insertArray[index] = itemElement
		}

		purchaseOrderItem := MongoSrv.Mongo().DB.Collection("purchase_order_item")
		GinRouter.ThrowErrorReturn(purchaseOrderItem.InsertMany(routerCtx, insertArray))

		response.DataResp(ctx, nil)
	})

	//erc客户更新采购订单
	//user客户 client供应商
	router.Put("/closePurchaseOrder", func(ctx *gin.Context) {
		type Payload struct {
			PurchaseDetailIds []int
			FinishState int
		}
		type ReqBody struct {
			Header interface{}
			Data Payload
		}
		var reqBody ReqBody
		GinRouter.ThrowError(ctx.BindJSON(&reqBody))

		purchaseOrderIds := len(reqBody.Data.PurchaseDetailIds)
		if purchaseOrderIds == 0 {
			panic("purchasedetail_id missing")
		}

		filter := bson.M{}
		filter["purchasedetail_id"] = bson.M{"$in": reqBody.Data.PurchaseDetailIds}

		updateData := bson.D{{
			"$set",
			bson.D{{
				"finish_state", reqBody.Data.FinishState,
			}},
		}}

		purchaseOrderItem := MongoSrv.Mongo().DB.Collection("purchase_order_item")
		res := GinRouter.ThrowErrorReturn(purchaseOrderItem.UpdateMany(routerCtx, filter, updateData))
		result := res.(*mongo.UpdateResult)

		response.DataResp(ctx, result)
	})

	//erc客户发送供货计划给供应商
	//user客户 client供应商
	router.Post("/sendSupplyPlan", func(ctx *gin.Context) {
		type Payload struct {
			SupplyPlanList []bson.M
		}
		type ReqBody struct {
			Header interface{}
			Data Payload
		}
		var reqBody ReqBody
		GinRouter.ThrowError(ctx.BindJSON(&reqBody))

		insertCount := len(reqBody.Data.SupplyPlanList)
		if insertCount == 0 {
			panic("no supply materiel plan")
		}

		insertArray := make([]interface{}, insertCount)
		for index, item := range reqBody.Data.SupplyPlanList {
			itemElement := bson.D{}
			for key, val := range item {
				itemElement = append(itemElement, bson.E{Key: key, Value: val})
			}
			insertArray[index] = itemElement
		}

		supplyMaterielPlan := MongoSrv.Mongo().DB.Collection("supply_materiel_plan")
		GinRouter.ThrowErrorReturn(supplyMaterielPlan.InsertMany(routerCtx, insertArray))

		response.DataResp(ctx, nil)
	})

	router.Post("/post/:id", func(ctx *gin.Context) {
		demoId := ctx.Param("demoId")
		queryStr := ctx.Query("query")

		type Payload struct {
			Name string
			Age int
		}

		payload := Payload{}
		err := ctx.BindJSON(&payload)
		if err != nil {
			panic(err.Error())
		}

		response.DataResp(ctx, gin.H{
			"demoId": demoId,
			"queryStr": queryStr,
			"name": payload.Name,
			"age": payload.Age,
		})
	})

	router.Post("/insert", func(ctx *gin.Context) {
		/*name := ctx.PostForm("name")
		if name == "" {
			panic("name missing")
		}*/

		type Payload struct {
			Name string
		}

		payload := Payload{}
		err := ctx.BindJSON(&payload)
		if err != nil {
			panic(err.Error())
		}

		demo := model.Demo{DemoName: payload.Name}
		result := GormSrv.GDB().Create(&demo)
		GormSrv.ThrowDBError(result)

		response.DataResp(ctx, demo)
	})

	router.Put("/update", func(ctx *gin.Context) {
		response.DataResp(ctx, gin.H{
			"message": "update",
		})
	})
	
	router.Delete("/delete", func(ctx *gin.Context) {
		response.DataResp(ctx, gin.H{
			"message": "delete",
		})
	})
}
